package cn.ecnu.service.impl;

import cn.ecnu.common.constant.BusinessConstant;
import cn.ecnu.mapper.ConsultRecordMapper;
import cn.ecnu.mapper.ConsultantMapper;
import cn.ecnu.pojo.ConsultRecord;
import cn.ecnu.pojo.Consultant;
import cn.ecnu.service.VisitorService;
import cn.ecnu.vo.fromFront.CommentConsultVo;
import cn.ecnu.vo.fromFront.EndConsultVo;
import cn.ecnu.vo.fromFront.StartConsultVo;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;

@Service
public class VisitorServiceImpl implements VisitorService {

    @Resource
    private ConsultRecordMapper consultRecordMapper;

    @Resource
    private ConsultantMapper consultantMapper;

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public HashMap<String, Integer> startConsult(StartConsultVo vo) {
        ConsultRecord consultRecord = new ConsultRecord();
        HashMap<String, Integer> map = new HashMap<>();
        try {
            // copy属性
            consultRecord.setFromId(vo.getFromId());
            consultRecord.setToId(vo.getToId());
            consultRecord.setDatetime(new Date());
            consultRecordMapper.insertConsultRecord(consultRecord);
            map.put("consultRecordId", consultRecord.getConsultRecordId());
            ListOperations<Object, Object> ops = redisTemplate.opsForList();
            String key = String.format(BusinessConstant.CONSULTANT_ONGOING_LIST_KEY, vo.getToId());
            ops.rightPush(key, vo.getFromId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public void endConsult(EndConsultVo vo) {
        try {
            ConsultRecord consultRecord = consultRecordMapper.getConsultRecordById(vo.getConsultRecordId());
            consultRecord.setDuration(vo.getDuration());
            consultRecordMapper.updateRecordById(consultRecord);
            Consultant consultant = consultantMapper.getConsultantById(consultRecord.getToId());
            consultant.setCountTotal(consultant.getCountTotal() + 1); consultant.setTimeTotal(consultant.getTimeTotal() + consultRecord.getDuration());
            consultantMapper.updateConsultant(consultant);
            String key = String.format(BusinessConstant.CONSULTANT_ONGOING_LIST_KEY, consultant.getConsultantId());
            redisTemplate.opsForList().remove(key, 0, consultRecord.getFromId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void cancelConsult(Integer rid) {
        try {
            ConsultRecord consultRecord = consultRecordMapper.getConsultRecordById(rid);
            consultRecordMapper.deleteConsultRecordById(rid);
            Consultant consultant = consultantMapper.getConsultantById(consultRecord.getToId());
            String key = String.format(BusinessConstant.CONSULTANT_ONGOING_LIST_KEY, consultant.getConsultantId());
            redisTemplate.opsForList().remove(key, 0, consultRecord.getFromId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void commentConsult(CommentConsultVo vo) {
        try {
            ConsultRecord consultRecord = consultRecordMapper.getConsultRecordById(vo.getConsultRecordId());
            consultRecord.setFromGrade(vo.getFromGrade());
            consultRecord.setFromComment(vo.getFromComment());
            consultRecordMapper.updateRecordById(consultRecord);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
